package com.qifan.energy.fragment;

import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.qifan.energy.R;
import com.qifan.energy.adapter.CategoryNewListAdapter;
import com.qifan.energy.adapter.OrderReadyShipAdapter;
import com.qifan.energy.base.BaseFragment;
import com.qifan.energy.bean.result.OrderItem;
import com.qifan.energy.bean.result.Page2;
import com.qifan.energy.bean.result.Pagination2;
import com.qifan.energy.http.net.NetUtils;
import com.qifan.energy.http.net.RequestParams;
import com.qifan.energy.http.response.MyObserver;
import com.qifan.energy.util.ToastUtils;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;

public class CategoryNewFragment extends BaseFragment {

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;

    private List<String> dataList = new ArrayList();
    private CategoryNewListAdapter adapter;
    private int pageNum = 1;

    public static CategoryNewFragment newInstance() {
        return new CategoryNewFragment();
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_category_recommend;
    }

    @Override
    protected void initView(View view) {

        adapter = new CategoryNewListAdapter(dataList);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(getActivity())
                .sizeResId(R.dimen.dp_0_5)
                .colorResId(R.color.divider_color)
                .build());

        /* 加载更多 */
        /*adapter.setOnLoadMoreListener(()->{
            orderListMore();
        },recyclerView);*/

        //添加空布局
        adapter.setEmptyView(getEmptyDataView());
    }

    @Override
    public void initData() {
        //orderList();

        dataList.add("dataList1");
        dataList.add("dataList2");
        dataList.add("dataList3");
        dataList.add("dataList4");
        adapter.setNewData(dataList);
    }

    /* 获取空布局 */
    private View getEmptyDataView() {
        return getLayoutInflater().inflate(R.layout.category_list_empty, recyclerView, false);
    }

    /* 下来刷新 */
    /*private void orderList(){
        pageNum = 1;
        NetUtils.orderList(RequestParams.ORDERTYPE.DAIFA, pageNum, Pagination2.PAGESIZE_10, new MyObserver<Page2<OrderItem>>(getActivity()) {
            @Override
            public void onSuccess(Page2<OrderItem> page2) {
                if (null != page2) {
                    dataList = page2.getList();
                    adapter.setNewData(page2.getList());
                }
            }

            @Override
            public void onFailure(Throwable e, String errorMsg) {
                ToastUtils.showShort(errorMsg);
            }
        });
    }*/

    /* 上拉加载 */
    /*private void orderListMore() {
        ++pageNum;
        NetUtils.orderList(RequestParams.ORDERTYPE.DAIFA, pageNum, Pagination2.PAGESIZE_10, new MyObserver<Page2<OrderItem>>(getActivity()) {
            @Override
            public void onSuccess(Page2<OrderItem> page2) {
                if (null != page2) {
                    dataList.addAll(page2.getList());
                    boolean isNoMore = (dataList.size() >= page2.getTotal());
                    adapter.setEnableLoadMore(!isNoMore);
                    if (isNoMore) {
                        adapter.loadMoreEnd();
                    } else {//刷新数据
                        adapter.setNewData(dataList);
                    }
                }
            }

            @Override
            public void onFailure(Throwable e, String errorMsg) {
                ToastUtils.showShort(errorMsg);
            }
        });
    }*/
}
